package com.arpa.ntocc.common.service.authorize;

import com.alibaba.fastjson.JSON;
import com.arpa.core.web.HttpRequest;

import java.util.HashMap;

/**
 * Create by LEO on 2017/10/28
 */
public class WeixinService {
    private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token";
    private static final String ACCESS_INFO_URL = "https://api.weixin.qq.com/sns/userinfo";


    /**
     * 获取微信授权token
     * @param appId
     * @param secret
     * @param code
     * @return
     */
    public static HashMap<String,Object> getAccessToken(String appId,String secret, String code){

        String param="secret="+secret+"&grant_type=authorization_code&code="+code+"&appid="+appId;
        String result = HttpRequest.sendGet(ACCESS_TOKEN_URL,param);
        HashMap<String,Object> map =  JSON.parseObject(result,HashMap.class);
        return map;
    }

    /**
     * 获取微信账号基本信息
     * @param token
     * @param openid
     * @return
     */
    public static HashMap<String,Object> getAccountInfo(String token,String openid){
        String param="access_token="+token+"&openid="+openid;
        String result = HttpRequest.sendGet(ACCESS_INFO_URL,param);
        HashMap<String,Object> map =  JSON.parseObject(result,HashMap.class);
        return map;
    }

}
